﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ISerivces;
using DWF.Studio.RobotCommunication;
using DWF.Studio.RobotCommunication.Enum;
using DWF.Studio.Share.Utils;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Services
{
    internal class RobotService : IRobotService
    {
        private IConfigService _configService = null;

        public RobotService()
        {
            _configService = new ConfigService();
        }

        public string GetLocalRobotPath()
        {
            var result = String.Empty;

            RegistryKey reg = Registry.CurrentUser;
            string regPath = @"SOFTWARE\ProcessGo\Robot\";
            RegistryKey regk = reg.OpenSubKey(regPath, true);

            if (regk != null)
            {
                var value = regk.GetValue("StudioStorage");
                if (value != null)
                {
                    var path = value.ToString();
                    var isExisting = FileHelper.CheckFolderExisting(path);
                    if (isExisting)
                    {
                        result = path;
                    }
                }
            }

            return result;
        }

        public bool IsExistLocalRobot()
        {
            var result = false;

            RegistryKey reg = Registry.CurrentUser;
            string regPath = @"SOFTWARE\ProcessGo\Robot\";
            RegistryKey regk = reg.OpenSubKey(regPath, true);

            if (regk != null)
            {
                var value = regk.GetValue("StudioStorage");
                if (value != null)
                {
                    var path = value.ToString();
                    FileHelper.CreateFolder(path);
                    result = FileHelper.CheckFolderExisting(path);
                }
            }

            return result;
        }

        public void NoticePublishProcess(string fullName)
        {
            var config = _configService.LoadAppConfig();
            var address = config.LocalRobotIP;
            var port = getPort();
            if (String.IsNullOrWhiteSpace(port))
                port = config.LocalRobotPort;

            using (var handler = new MsgHandler(address, port))
            {
                RetMessage rm = new RetMessage();
                rm.dataList.Add(fullName);
                rm.bResult = true;

                ProcessMsg msg = new ProcessMsg(new Message());
                msg.comMsg.nMsgID = MessageID.Studio_Notify_UpdateScript;
                msg.comMsg.strMsgName = MessageID.Studio_Notify_UpdateScript.ToString();
                msg.comMsg.AddArg(MsgTransDes.TransMsg.ToString(), rm);

                handler.Send(msg);
            }
        }

        private string getPort()
        {
            RegistryKey reg = Registry.CurrentUser;
            string regPath = @"SOFTWARE\ProcessGo\Robot";
            RegistryKey rkey = reg.OpenSubKey(regPath, true);

            if (rkey == null)
            {
                return null;
            }

            var obj = rkey.GetValue("HostPort_FromStudioMsg");

            if (obj == null)
            {
                return null;
            }
            else
            {
                return obj.ToString();
            }
        }
    }
}
